package com.bcgtgjyb.huanwen.customview.mylibrary.spring;

import com.bcgtgjyb.huanwen.customview.mylibrary.ResourceTable;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

/**
 * GameMap 类
 *
 * @since 2016-04-25
 */
public class GameStart extends DirectionalLayout implements Component.TouchEventListener {
    private Context mContext;
    private GameMap mGameMap;
    private String TAG = GameStart.class.getName();
    private SpringView mSpringView;
    private boolean isMove = false;
    private float downX,downY;
    private double lastD = 0;
    /**
     * GameStart 构造方法
     *
     * @param context 内容
     */

    public GameStart(Context context) {
        super(context);
        mContext = context;
        init();
    }

    /**
     * GameStart 构造方法
     *
     * @param context 内容
     * @param attrs 属性
     */

    public GameStart(Context context, AttrSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        setTouchEventListener(this::onTouchEvent);
        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_game_start, this, false);
        mGameMap = (GameMap) findComponentById(ResourceTable.Id_game_map);
        mSpringView = (SpringView) findComponentById(ResourceTable.Id_springView);
        mGameMap.setSpringCallback(new GameMap.SpringCallback() {
            @Override
            public void changePoint() {
                mSpringView.addPoint();
            }

            @Override
            public void canvasScale(float parma) {
            }
        });
    }

    /**
     * 实现直角坐标系转换为极坐标系的方法
     *
     * @param sx x轴
     * @param sy y轴
     * @return 0
     */
    public double RectToPolar(double sx, double sy) {
        return Math.atan2(sy, sx);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent event) {
        float xx = event.getPointerPosition(0).getX();
        float yy = event.getPointerPosition(0).getY();
        if (event.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
            downX = xx;
            downY = yy;
            isMove = true;
            return true;
        }
        if (event.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
            if (isMove) {
                double dd = RectToPolar(xx - downX, yy - downY);
                mGameMap.move(dd);
                mSpringView.setDegree(dd);
                lastD = dd;
            }
        }
        return false;
    }
}
